const Router = require("koa-router");

const {
  userValidator,
  verifyUser,
  cryptPassword,
  verifyLogin,
} = require("../middleware/user.middleware");
const { auth } = require("../middleware/auth.middleware");
const {
  register,
  login,
  changePassword,
} = require("../controller/user.controller");

// prefix 自动拼接前缀
const router = new Router({ prefix: "/api/user" });

/**
 * @swagger
 * tags:
 *   name: Users
 *   description: User management
 */

// 注册接口: /api/user/register
/**
 * @swagger
 * /users:
 *   get:
 *     summary: Retrieve a list of users
 *     tags: [Users]
 *     responses:
 *       200:
 *         description: A list of users.
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/model/user.model.js'
 */
router.post("/register", userValidator, verifyUser, cryptPassword, register);

// 登录接口: /api/user/login
router.post("/login", verifyLogin, login);

// 登录接口: /api/user/login
router.post("/update", auth, cryptPassword, changePassword);

module.exports = router;
